import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import {InfoProvider} from "../../providers/InfoProvider";
import {HttpService} from "../../providers/HttpService";
import {CallNumber} from "@ionic-native/call-number";
import {CommProvider} from "../../providers/CommProvider";

/**
 * Generated class for the MemberPage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
  selector: 'page-member',
  templateUrl: 'member.html',
})
export class MemberPage {

  member_data: any = {
      id: null,
      phone: null,
      name: null,
      qq: null,
      gender: null
  };

  edit_type: number = 0;
  label_array: Array<any> = [
      {
          title: '成为会员',
          submit: '提交申请'
      },
      {
          title: '用户信息',
          submit: '修改信息'
      },
  ];

  constructor(public navCtrl: NavController, public navParams: NavParams,
              private callNumber: CallNumber,
              private infoProvider: InfoProvider,
              private commProvider: CommProvider,
              private httpService: HttpService) {
      let userInfo = this.infoProvider.getUserInfo();
      this.member_data.id = userInfo.id;
      this.member_data.phone = userInfo.phone;
      this.member_data.name = userInfo.name;
      this.member_data.qq = userInfo.qq;
      this.member_data.gender = userInfo.gender;

      let editType = this.navParams.get('edit_type');
      if (editType) {
          this.edit_type = editType;
      }
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad MemberPage');
  }

  onApplySubmit() {
      if (!this.member_data.name || !this.member_data.qq) {
          this.commProvider.showToast('请完善用户信息');
          return;
      }

      let postData = {
          name: this.member_data.name,
          qq: this.member_data.qq,
          gender: this.member_data.gender,
          extra: null
      };
      if (this.edit_type < 1) {
          postData.extra = 'apply';
      }
      this.httpService.instance()
          .setUrl('/user/vipApplyUser/' + this.member_data.id)
          .setBody(postData)
          .putRequest()
          .then(res => {
              if (res.status == 0) {
                  this.commProvider.showToast('信息已经提交');

                  this.httpService.instance()
                      .setUrl('/user/getUserInfo/' + this.member_data.id)
                      .getRequest()
                      .then(res => {
                          if (res.status == 0) {
                              this.infoProvider.setUserInfo(res.data);
                              this.navCtrl.pop();
                          }
                      });
              }
          });
  }

  onCallService() {
      let mobileConfig = this.infoProvider.getMobileConfig();
      console.log('mobileConfig:', mobileConfig);
      this.callNumber.callNumber(mobileConfig.service_phone, true)
          .then(() => console.log('Launched dialer!'))
          .catch(() => console.log('Error launching dialer'));
  }
}
